layui.use(['form', 'table'], function () {
    let $ = layui.jquery,
        form = layui.form,
        table = layui.table
        ,cate=$("input[name='cate']").val()
        ,hidde=true,
        table_render={
            elem: '#currentTableId',
            url: '/admin/adverbanner/index?action=ajax&cate='+cate,
            method: 'post',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter',],
            cols: [[
                // {type: "checkbox",},
                {field: 'title', title: '名称'},
                {field: 'imgurl', title: '查看图片', templet:"#logoTpl"},
                {field: 'start_time', title: '上架时间'},
                {field: 'end_time', title: '下架时间'},
                {field: 'bizpro_status', title: '是否关联',hide:hidde},//,templet:"#linkTpl"
                {field: 'show_type', title: '展示类型',hide:!hidde,templet:"#showTypeTpl"},
                {field: 'advert_type', title: '位置',templet:"#advertTypeTpl"},
                {field: 'status', title: '状态', templet:"#statusTpl",hide:hidde},
                {title: '操作', toolbar: '#currentTableBar', minWidth: 120}
            ]],
            //limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        };

    table.render(table_render);
    if(parseInt(cate)==1){
        //首页
        hidde=false
        table_render.cols[0][4].hide=hidde
        table_render.cols[0][5].hide=!hidde
        table_render.cols[0][7].hide=!hidde
    }else{
        hidde=true
        table_render.cols[0][4].hide=hidde
        table_render.cols[0][5].hide=!hidde
        table_render.cols[0][7].hide=!hidde

    }

    table.render(table_render);




    /**
     * toolbar监听事件
     */
    table.on('toolbar(currentTableFilter)', function (obj) {

        if (obj.event === 'add') {  // 监听添加操作
            let index = layer.open({
                title: '添加广告',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                area: ['80%', '80%'],
                content: '/admin/adverbanner/Saveinfo?cate='+cate
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        }



    });

    table.on('tool(currentTableFilter)', function (obj) {
        let data = obj.data;


        if (obj.event === 'delete') {

            layer.confirm('确认删除么？', function (index) {

                $.ajax({
                    url: '/admin/adverbanner/DelInfo',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        id: data.id,

                    },
                    success: function(data) {
                        if (data.status) {
                            layer.msg('删除成功!', {icon: 1, time: 1000},function () {
                                $(obj)[0].tr.remove()
                                // location.reload()
                            });
                        }else{
                            layer.msg(data.msg, {
                                icon: 2,
                                time: 3000
                            });

                        }

                    },
                    error: function(re) {

                        layer.open({
                            title: '删除失败',
                            content: '<span style="color:red">'+re.responseJSON.errorCode+':</span>'+re.responseJSON.msg
                        })
                    }
                });
                // obj.del();
                // layer.close(index);
            });
        }

        if(obj.event === 'thumb'){

            let aa={
                "title": data.title, //相册标题
                "id": 1, //相册id
                "start": 0, //初始显示的图片序号，默认0
                "data": [   //相册包含的图片，数组格式
                    {
                        "alt": data.nickname,
                        "pid": 2, //图片id
                        "src": data.imgsrc, //原图地址
                        "thumb": data.imgsrc //缩略图地址
                    }
                ]
            }
            layer.photos({
                photos: aa
                ,anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
            });

        }

        if (obj.event === 'edit') {

            console.log(data)

            let index = layer.open({
                title: '广告详情',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '/admin/adverbanner/Saveinfo?cate='+data.advert_type+'&id='+data.id,
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        }
    });

    function delajax(ids,callback) {

        $.ajax({
            url: '/admin/Industry/DelInfo',
            type: 'post',
            dataType: 'json',
            data: {
                id: ids,
            },
            success: function(data) {
                callback(data)
            },
            error: function(re) {
                console.log(re)
                layer.open({
                    title: '删除失败',
                    content: '<span style="color:red">'+re.responseJSON.errorCode+':</span>'+re.responseJSON.msg
                })
            }
        });
    }


});
